package com.slkj.mapper;

import com.slkj.entity.CityAudit;
import com.sun.org.apache.bcel.internal.generic.IFNULL;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface CityAuditMapper {
    //插入到审核表中关键信息
    @Insert("insert into city_audit(user_id,utilname,c_id,n_id,title,content,material,begintime,submit_materials,situation,audit,point,light,why,year,endtime)" +
            "values(#{userId},#{utilName},#{cid},#{nid},#{title},#{content},#{material},#{beginTime},#{submitMaterials},#{situation},#{audit},#{point},#{light},#{why},#{year},#{endTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(CityAudit cityAudit);

    //模糊查询
    @Select("SELECT \n" +
            "            *\n" +
            "    FROM\n" +
            "    city_audit audit\n" +
            "    INNER JOIN\n" +
            "    city city ON city.id = audit.c_id\n" +
            "            WHERE\n" +
            "    city.username = #{username}\n" +
            "    OR audit.user_id = #{selectUserId}\n" +
            "    AND audit.title LIKE CONCAT('%',IFNULL(#{title}, audit.`title`), '%') AND\n" +
            "    audit.utilname LIKE CONCAT('%',IFNULL(#{untilName},audit.`utilname`),'%') AND audit.light LIKE CONCAT('%',IFNULL(#{light},audit.`light`),'%') AND IFNULL(#{beginDate},audit.`begintime`)>=begintime\n" +
            "    AND IFNULL(#{endDate},audit.`endtime`)<=endtime AND audit.'audit'=IFNULL(#{status},audit.'audit') order by audit.id desc")
    List<CityAudit> showAll(@Param("title") String title, @Param("untilName") String untilName, @Param("light") String light, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate, @Param("username") String username, @Param("selectUserId") String selectUserId, @Param("status") String status);


    //审核打分
    @Update("update city_audit set point=#{point} where id=#{id}")
    int point(@Param("point") Integer point, @Param("id") String id);

    //根据id查询审核状态
    @Select("select audit from city_audit where id=#{id}")
    String showAllAudit(@Param("id") String id);

    //通过
    @Update("update city_audit set audit=#{audit},light=#{light} where id=#{id}")
    int audit(@Param("audit") String audit, @Param("light") String light, @Param("id") String id);

    //驳回
    @Update("update city_audit set audit=#{audit},light=#{light} where id=#{id}")
    int worry(@Param("audit") String audit, @Param("light") String light, @Param("id") String id);

    //亮灯调整
    @Update("update city_audit set audit=#{audit},light=#{light},why=#{why} where id=#{id}")
    public int why(@Param("audit") String audit, @Param("light") String light, @Param("why") String why, @Param("id") String id);


    @Select("select * from city_audit where id=#{id}")
    CityAudit selectByDutyId(String dutyId);

    @Update("update city_audit set submit_time=#{param1} where id=#{param2}")
    void updateSubmitTime(Date date, String dutyId);

//    //已完结
//    @Select("SELECT * FROM city_audit WHERE title LIKE CONCAT('%',ifnull(#{title},title),'%') AND " +
//            "utilname LIKE CONCAT('%',ifnull(#{untilName},utilname),'%') AND light=ifnull(#{light},light) AND ifnull(#{beginTime},begintime)>=begintime" +
//            " AND ifnull(#{endTime},endtime)<=endtime AND ifnull(#{month},MONTH(begintime))=MONTH(begintime) and audit='通过' LIMIT #{begin},#{pageSize}")
//    public List<CityAudit> showAllOk(@Param("title") String title, @Param("untilName") String untilName, @Param("light") String light, @Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("month") Integer month, @Param("begin") Integer begin, @Param("pageSize") Integer pageSize);
//
//    //已完结得数量
//    @Select("SELECT count(*) FROM city_audit WHERE title LIKE CONCAT('%',ifnull(#{title},title),'%') AND utilname LIKE CONCAT('%',ifnull(#{untilName},utilname),'%') AND light=ifnull(#{light},light) AND ifnull(#{beginTime},begintime)>=begintime AND ifnull(#{endTime},endtime)<=endtime AND ifnull(#{month},MONTH(begintime))=MONTH(begintime) AND audit='通过'")
//    public int countOk(@Param("title") String title, @Param("untilName") String untilName, @Param("light") String light, @Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("month") Integer month);
//

}
